<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/23
 * Time: 17:03
 */

namespace app\common\model;


use think\Model;

class System extends Model
{
    protected $autoWriteTimestamp = true;

    protected $auto   = [];
    protected $insert = ['group' => 4];
    protected $update = [];

    /**
     * 添加配置项
     * @param $data 输入的数据
     * @return $this
     */
    public function itemAdd($data)
    {
        $num = $this->max('sort');
        $num = $num + 1;
        $data['sort'] = $num;
        $this->data($data);
        $this->allowField(true)->isUpdate(false)->save();
        return $this;
    }

    /**
     * 保存配置信息
     * @param $data 输入的数据
     * @return $this
     */
    public function saveConfig($data)
    {
        foreach ($data as $key => $value) {
            $this->where('cfg_name', $key)->update(['cfg_value'=>$value, 'update_time'=>time()]);
        }
        return $this;
    }
}